package com.wing.unit.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.unit.dao.AgentDueSubjectDao;
import com.wing.unit.model.entity.AgentDueSubject;
import com.wing.unit.model.request.AgentDueSubjectForm;
import com.wing.unit.model.response.AgentDueSubjectVO;
import com.wing.unit.model.response.CustomerDueSubjectVO;
import com.wing.unit.model.response.NodeDataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理商渠道尽调科目表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class AgentDueSubjectServiceImpl extends ServiceImpl<AgentDueSubjectDao, AgentDueSubject> implements AgentDueSubjectService {

    @Override
    public int add(AgentDueSubjectForm agentDueSubjectForm) {

        if(agentDueSubjectForm!=null&&agentDueSubjectForm.getIsFinal()==null){
            agentDueSubjectForm.setIsFinal(false);
        }
        //校验

        AgentDueSubject agentDueSubject =new AgentDueSubject();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(AgentDueSubject.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(agentDueSubjectForm, agentDueSubject, copyOptions);

        return baseMapper.insert(agentDueSubject);
    }

    @Override
    public List<AgentDueSubjectVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<AgentDueSubjectVO> pageList4app(Page page, Map<String, Object> params) {
        List<AgentDueSubjectVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public AgentDueSubjectVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public JsonResult getTreeData() {

        List<AgentDueSubjectVO> list=baseMapper.list4app(new HashMap<>());
        Map<Long,List<NodeDataVO>> map=new HashMap<>();
        for (AgentDueSubjectVO agentDueSubjectVO:list) {
            if (map.containsKey(agentDueSubjectVO.getParentId())){
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(agentDueSubjectVO.getId().toString());
                nodeDataVO.setName(agentDueSubjectVO.getName());
                map.get(agentDueSubjectVO.getParentId()).add(nodeDataVO);
            }else{
                List<NodeDataVO> tmpList=new ArrayList<>();
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(agentDueSubjectVO.getId().toString());
                nodeDataVO.setName(agentDueSubjectVO.getName());
                tmpList.add(nodeDataVO);
                map.put(agentDueSubjectVO.getParentId(),tmpList);
            }
        }
        List<NodeDataVO> rootList=map.get(-1L);
        if (rootList==null){
            return JsonResult.success();
        }
        constructTree(rootList,map);
        return JsonResult.success(rootList);
    }

    private void  constructTree(List<NodeDataVO> parentList,Map<Long,List<NodeDataVO>> dataMap){
        for (NodeDataVO node:parentList) {
            Long nodeId=Long.valueOf(node.getValue());
            List<NodeDataVO> childList=dataMap.get(nodeId);
            if (childList!=null){
                node.setChildren(childList);
                constructTree(childList,dataMap);
            }
        }
    }
}
